package lmteam.userservice;

import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertFalse;

import lmteam.common.*;

public class UserServiceProcessorTest
{
    
    @Test
    public void testUserGetFunction()
	throws java.rmi.RemoteException
    {
	UserServiceProcessor usp = new UserServiceProcessor();

	User user;

	user = usp.get("tom");
       
	assertNotNull( "user is null", user );
	// these are dependent on the standard test db
	assertTrue( (user.getUsername().compareTo("tom") == 0) );
	assertTrue( (user.getPassword().compareTo("tom") == 0) );
	assertTrue( (user.getRole().isApprover() == true ) );
	assertTrue( (user.getRole().isReviewer() == true ) );
	assertTrue( (user.getRole().isSubmitter() == true ) );

	user = usp.get("subs");

	assertNotNull( "user is null", user );
	// these are dependent on the standard test db
	assertTrue( (user.getUsername().compareTo("subs") == 0) );
	assertTrue( (user.getPassword().compareTo("subs") == 0) );
	assertTrue( (user.getRole().isApprover() == false ) );
	assertTrue( (user.getRole().isReviewer() == false ) );
	assertTrue( (user.getRole().isSubmitter() == true ) );

	user = usp.get("revsjr");

	assertNotNull( "user is null", user );
	// these are dependent on the standard test db
	assertTrue( (user.getUsername().compareTo("revsjr") == 0) );
	assertTrue( (user.getPassword().compareTo("revsjr") == 0) );
	assertTrue( (user.getRole().isApprover() == false ) );
	assertTrue( (user.getRole().isReviewer() == true ) );
	assertTrue( (user.getRole().isSubmitter() == false ) );

	user = usp.get("nobody");
	
	assertNull("user is not null", user );
    }

    @Test
    public void testUserCreateAndDeleteFunctions()
	throws java.rmi.RemoteException
    {
	UserServiceProcessor usp = new UserServiceProcessor();

	assertTrue( usp.delete( "somebody" ) );	

	assertTrue( usp.create("somebody", "somebody" ) );
	assertFalse( usp.create("somebody", "somebld" ) );

	assertTrue( usp.delete( "somebody" ) );
    }

    @Test
    public void testUserUpdatesPassword()
	throws java.rmi.RemoteException
    {
	UserServiceProcessor usp = new UserServiceProcessor();

	assertTrue( usp.delete( "somebody" ) );	

	assertTrue( usp.create("somebody", "somebody" ) );
	
	User user = usp.get("somebody");
	assertNotNull("got a null", user );

	assertTrue( usp.update( user, "fakepasswd" ) );

	user = usp.get("somebody");
	assertNotNull( "got a null v2", user );
	
	assertTrue( (user.getPassword().compareTo( "fakepasswd" ) == 0) );

	assertTrue( usp.delete( "somebody" ) );
	
    }

    @Test
    public void testUserUpdatesRoles()
	throws java.rmi.RemoteException
    {
	UserServiceProcessor usp = new UserServiceProcessor();

	assertTrue( usp.delete( "somebody" ) );	

	assertTrue( usp.create("somebody", "somebody" ) );
	
	User user = usp.get("somebody");
	assertNotNull("got a null", user );

	Role role = new Role( false, true, true );

	assertTrue( usp.update( user, role, true ) );

	user = usp.get("somebody");
	assertNotNull( "got a null v2", user );
	
	//assertTrue( (user.getPassword().compareTo( "fakepasswd" ) == 0) );
	assertFalse( user.getRole().isApprover() );
	assertTrue( user.getRole().isReviewer() );
	assertTrue( user.getRole().isSubmitter() );

	assertTrue( usp.delete( "somebody" ) );	
    }
    
    @Test
    public void testgetUsers()
	throws java.rmi.RemoteException
    {
	UserServiceProcessor usp = new UserServiceProcessor();

	User[] users = usp.getUsers();
	
	assertNotNull( users );

	System.out.println("Users.length = " + users.length );
	assertTrue( users.length == 10 );

	assertTrue( usp.create("somebody", "somebody" ) );
	assertTrue( usp.create("somebody1", "somebody1" ) );
	assertTrue( usp.create("somebody2", "somebody2" ) );

	users = usp.getUsers();

	assertNotNull( users );
	System.out.println("Users.length = " + users.length );
	assertTrue( users.length == 13 );
	

	assertTrue( usp.delete( "somebody" ) );	
	assertTrue( usp.delete( "somebody1" ) );	
	assertTrue( usp.delete( "somebody2" ) );	

    }
}